<?php
	class Shepherd_AjaxController extends Sss_Controller_Shepherd
	{
		public function indexAction()
		{
			$method    = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::METHOD);
			$returnVal = '';
			if (isset($method)) {
				switch ($method) {
					case Sss_Ultility_AjaxMethodName::GET_HARDWARE_BY_ALLOCATION:
						$allocationId = $this->getRequest()->getParam('allocationid');
						if (isset($allocationId)) {
							$idVal = explode('-', $allocationId);
							$returnVal = $this->_getHardwareForSelectBox($idVal[0], $idVal[1]);
						}
						break;
					case Sss_Ultility_AjaxMethodName::GET_HARDWARE_VALUE_BY_HARDWARE:
						$hardwareid = $this->getRequest()->getParam('hardwareid');
						if (isset($hardwareid)) {
							$returnVal = $this->_getHardwareValueTypeByHardwareForSelectBox($hardwareid);
						}
						break;
					case Sss_Ultility_AjaxMethodName::GET_CHART_BY_HARDWARE:
						$hardwareid = $this->getRequest()->getParam('hardwareid');
						if (isset($hardwareid)) {
							$returnVal = $this->_getChartForSelectBox($hardwareid);
						}
						break;
					case Sss_Ultility_AjaxMethodName::GET_GRAPHS_BY_CHART:
						$chartId = $this->getRequest()->getParam('chartid');
						if(isset($chartId))
						{
							$returnVal = $this->_getGraphsByChart($chartId);
						}
						break;
					case Sss_Ultility_AjaxMethodName::GET_CHART_INFORMATION:
						$chartId = $this->getRequest()->getParam('chartid');
						if(isset($chartId)) {
							$returnVal = $this->_getChartInformation($chartId);
						}
						break;
					case Sss_Ultility_AjaxMethodName::UPDATE_CHART_STATUS:
						$chartId = $this->getRequest()->getParam('chart');
						$showed = $this->getRequest()->getParam('showed');
						$this->_updateChartStatus($chartId, $showed);
						break;
					case Sss_Ultility_AjaxMethodName::UPDATE_CHART_STATE:
						$chartId = $this->getRequest()->getParam('chart');
						$this->_updateChartState($chartId);
						break;
					case Sss_Ultility_AjaxMethodName::GET_ROOMS_BY_STOCKLOCATION:
						$stockLocation = $this->getRequest()->getParam('stocklocation', -1);
						if (isset($stockLocation)) {
							$returnVal = $this->_getRoomsByStockLocation($stockLocation);
						}
						break;
					case Sss_Ultility_AjaxMethodName::GET_CHARTS_BY_PRESET:
						$presetId = $this->getRequest()->getParam('presetid', -1);
						if (isset($presetId)) {
							$returnVal = $this->_getChartsByPreset($presetId);
						}
						break;
				};
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::HTML_CONTENT, $returnVal);
		}

		private function _getHardwareForSelectBox($destinationId, $systemName)
		{
			$hardware  = new Application_Model_Mapper_Hardware();
			$list      = $hardware->getSensorList($destinationId, $systemName);
			$returnVal = '';
			if (isset($list)) {
				foreach ((array) $list as $hrd) {
					$returnVal .= "<option value='" . $hrd['hardwareid'] . "'>" . $hrd['hardwarename'] . '-' . $hrd['serial_number'] . "</option>";
				}
			}
			return $returnVal;
		}

		private function _getChartForSelectBox($hadwareId)
		{
			$chartTable = new Shepherd_Model_Mapper_Chart(); 
			$list       = $chartTable->getChartListByHardware($hadwareId);
			$returnVal = array();
			if (isset($list)) {
				foreach ((array) $list as $chart) {
					$fromDateTimme       = (int)$chart['time_range_type'] == 3?date(DATETIMEFORMAT, $chart['from_datetime']):'';
					$toDateTimme         = (int)$chart['time_range_type'] == 3?date(DATETIMEFORMAT, $chart['to_datetime']):'';
					$returnVal[] = $chart['chartid'].';'.$chart['chartname'].';'.$chart['typename'].';'.$fromDateTimme.';'.$toDateTimme;  
				}
			}
			return count($returnVal)>0?implode('|', $returnVal):'';
		}

		private function _getHardwareValueTypeByHardwareForSelectBox($hardwareid)
		{
			$hardware  = new Application_Model_Mapper_Hardware();
			$list      = $hardware->getHardwareValueTypeList($hardwareid);
			$returnVal = '';
			if (isset($list)) {
				foreach ((array) $list as $sys) {
					$returnVal .= "<option value='" . $sys['systemtypeid'] . "'>" . $sys['typename'] . "</option>";
				}
			}
			return $returnVal;
		} 

		private function _getGraphsByChart($chartId)
		{
			$graphTable  = new Shepherd_Model_Mapper_Graph(); 
			$list        = $graphTable->getGraphListByChart($chartId);
			$returnVal = '';
			if (isset($list)) {
				foreach ((array) $list as $sys) {
					$returnVal .= "<option value='" . $sys['graphid'] . "'>" . $sys['graphname'] . "</option>";
				}
			}
			return $returnVal;
		}

		private function _getChartInformation($chartId)
		{
			$chartTable = new Shepherd_Model_Mapper_Chart();
			$chart = $chartTable->findEntityById(array('chartid'=>$chartId));
			$returnVal = '';
			if (!is_null($chart)) {
				$fromDate = '';
				$toDate = '';
				$timeRangeType =  '';   
				switch ($chart->time_range_type) {
					case 1:
						$timeRangeType = 'Now';
						break;
					case 2:
						$timeRangeType = 'Today';
						break;
					case 3:
						$timeRangeType = 'Other';
						$fromDate = date(DATETIMEFORMAT, $chart->from_datetime);
						$toDate = date(DATETIMEFORMAT, $chart->to_datetime);
						break;	
				};
				$returnVal = $timeRangeType . ';' .  $fromDate . ';' . $toDate;
			}
			return $returnVal;
		}

		private function _updateChartStatus($chartId, $showed)
		{
			$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
			if (!is_null($userInformation->accountid)) {
				$chartSessionTable = new Shepherd_Model_Mapper_ChartSession();
				$existedChart = $chartSessionTable->getSavedChartByAccount($chartId, $userInformation->accountid);
				if (!empty($existedChart)) { 
					$chart = array(
					'showed'=>$showed
					);
					$chartSessionTable->updateData(array('chart'=>$chartId, 'account'=>$userInformation->accountid, ), $chart, true);
				}
			}
		}

		private function _updateChartState($chartId)
		{
			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
			$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
			$instanceId = $defaultOvisNamespace->dbShepherd;
			if (!is_null($userInformation->accountid)) {
				$chartSessionTable = new Shepherd_Model_Mapper_ChartSession();
				$chartSessionContructor = new Shepherd_Model_ChartSession();
				$existedChart = $chartSessionTable->getSavedChartByAccount($chartId, null);
				if (empty($existedChart)){
					$chartSessionContructor->setChart($chartId);
					$chartSessionContructor->setAccount($userInformation->accountid);
					$chartSessionContructor->setRemoved(0);
					$chartSessionContructor->setShowed(0);
					$chartSessionTable->save($chartSessionContructor,$instanceId);
				} else {
					$chartSessionData = $chartSessionTable->findById($chartId,'chart',$instanceId);
					$chartSessionData->setAccount($userInformation->accountid);
					$chartSessionData->setRemoved(0);
					$chartSessionTable->save($chartSessionData,$instanceId);
				}
			}
		}
		private function _getRoomsByStockLocation($stockLocationId){
			$containerTable   = new Shepherd_Model_Mapper_Container();
			if($stockLocationId == -1){
				$rooms = $containerTable->getAllRoomByIsUsed();
			}else{
				$rooms = $containerTable->getAllRoomByIsUsed($stockLocationId);	
			}
			if (isset($rooms)) {
				foreach ((array) $rooms as $sys) {
					$returnVal .= "<option value='" . $sys['containerid'].'-'.Sss_Ultility_SystemName::ROOMS . "'>" . $sys['containername'] . "</option>";
				}
			}
			return $returnVal;
		}
	
		private function _getChartsByPreset($presetId)
		{
			$presetChartTable  = new Shepherd_Model_Mapper_PresetChart();
			$charts = $presetChartTable->getChartListByPresetId($presetId);
			$returnVal = '';
			if (isset($charts)) {
				foreach ((array) $charts as $sys) {
					$returnVal .= "<option value='" . $sys['presetchartid'] . "'>" . $sys['chartname'] . "</option>";
				}
			}
			return $returnVal;
		}
	}
